1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.48">
9 <style type=
"text/css">
10 p
.p1
{margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica
}
11 p
.p2
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
}
12 p
.p3
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; min-height: 14.0px}
13 p
.p4
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #bf0000}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #0000bf}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica
; color: #0000bf}
19 span
.s1
{text-decoration: underline
; color: #0000bf}
20 span
.s2
{color: #0000bf}
21 span
.s3
{color: #000000}
22 span
.s4
{color: #0000bf}
23 span
.s5
{color: #007300}
24 span
.s6
{color: #0000bf}
25 span
.s7
{font: 12.0px Helvetica
}
26 span
.Apple-tab-span
{white-space:pre
}
30 <p class=
"p1"><b>Adding values to a base event pattern
</b></p>
31 <p class=
"p2"><b>(Or, “Pattern Composition”)
</b></p>
32 <p class=
"p3"><br></p>
33 <p class=
"p4">One way to use patterns is to write everything into the pattern up front. This has the advantage of clarity and ease of understanding. Another way is to modularize the behavior by creating smaller, simpler patterns and combining their results into single events that have keys and values from all the component patterns.
</p>
34 <p class=
"p3"><br></p>
35 <p class=
"p4">This is related to the computer science concept of
"function composition," in which a complex calculation can be written not as a single large function, but as several smaller functions that are then chained together into a single function. Since Functions are normal objects in SuperCollider, it's easy to do an operation on a function that returns a composite function (which may then be used like any other function).
<a href=
"http://en.wikipedia.org/wiki/Function_composition_(computer_science)"><span class=
"s1">http://en.wikipedia.org/wiki/Function_composition_(computer_science)
</span></a></p>
36 <p class=
"p3"><br></p>
37 <p class=
"p4">In mathematics, the
<b>·
</b> operator represents function composition.
</p>
38 <p class=
"p3"><br></p>
39 <p class=
"p4"><span class=
"Apple-tab-span"> </span>f(x) = x +
1</p>
40 <p class=
"p4"><span class=
"Apple-tab-span"> </span>g(x) = x *
2</p>
41 <p class=
"p3"><span class=
"Apple-tab-span"> </span></p>
42 <p class=
"p4"><span class=
"Apple-tab-span"> </span>g · f = g(f(x)) = (x +
1) *
2</p>
43 <p class=
"p3"><br></p>
44 <p class=
"p4">g · f means to evaluate f first, then pass its result to g. The · operator is written as
<> in SuperCollider.
</p>
45 <p class=
"p3"><br></p>
46 <p class=
"p5">f = {
<span class=
"s2">|x|
</span> x +
1 };
</p>
47 <p class=
"p5">g = {
<span class=
"s2">|x|
</span> x *
2 };
</p>
48 <p class=
"p6"><br></p>
49 <p class=
"p5">h = (g
<> f);
</p>
50 <p class=
"p5">--
> a Function
</p>
51 <p class=
"p6"><br></p>
52 <p class=
"p5">h.value(
1);
</p>
53 <p class=
"p7"><span class=
"s3">--
> 4<span class=
"Apple-tab-span"> </span></span>// == (
1+
1) *
2</p>
54 <p class=
"p6"><br></p>
55 <p class=
"p5">(f
<> g).value(
1)
</p>
56 <p class=
"p7"><span class=
"s3">--
> 3<span class=
"Apple-tab-span"> </span></span>// == (
1*
2) +
1</p>
57 <p class=
"p6"><br></p>
58 <p class=
"p7">// g · f == g(f(x)) -- f is evaluated first, and its result is passed to g
</p>
59 <p class=
"p5">g.value(f.value(
1));
</p>
60 <p class=
"p5">--
> 4</p>
61 <p class=
"p3"><br></p>
62 <p class=
"p4">Event patterns can be similarly composed.
</p>
63 <p class=
"p3"><br></p>
65 <li style=
"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pbindf(pattern, pairs):
</b> Adds new key-value pairs onto a pre-existing Pbind-style pattern. Pbindf(Pbind(\a, patternA), \b, patternB, \c, patternC) gets the same result as Pbind(\a, patternA, \b, patternB, \c, patternC).
</li>
66 <li style=
"margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica"><b>Pchain(patterns):
</b> Chains separate Pbind-style patterns together, so that all their key-value pairs go into the same event. For example, if one part of your code creates a Pbind instance a = Pbind(\a, patternA) and another part creates b = Pbind(\b, patternB, \c, patternC), you could append \b and \c into the \a result using Pchain(b, a). The subpatterns evaluate in reverse order, in keeping with function composition notation.
</li>
68 <p class=
"p3"><br></p>
69 <p class=
"p4">For musical purposes, you could have one part of your code create a pattern defining rhythm and another part defining pitch material, then combine them with
<a href=
"../Patterns/Pchain.html"><span class=
"s4">Pchain
</span></a>.
</p>
70 <p class=
"p3"><br></p>
71 <p class=
"p5">~rhythm =
<span class=
"s2">Pbind
</span>(
</p>
72 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s5">\dur
</span>,
<span class=
"s2">Pwrand
</span>(#[
0.125,
0.25,
0.5], #[
0.3,
0.5,
0.2],
<span class=
"s2">inf
</span>),
</p>
73 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s5">\legato
</span>,
<span class=
"s2">Pwrand
</span>(#[
0.1,
0.6,
1.01], #[
0.1,
0.3,
0.6],
<span class=
"s2">inf
</span>)
<span class=
"Apple-converted-space"> </span></p>
75 <p class=
"p5">~melody =
<span class=
"s2">Pbind
</span>(
</p>
76 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s5">\degree
</span>,
<span class=
"s2">Pwhite
</span>(-
4,
11,
<span class=
"s2">inf
</span>)
</p>
78 <p class=
"p6"><br></p>
79 <p class=
"p5">p =
<span class=
"s2">Pchain
</span>(~melody, ~rhythm).play;
</p>
80 <p class=
"p5">p.stop;
</p>
81 <p class=
"p3"><br></p>
82 <p class=
"p4">That in itself has some good potential for algorithmic composition. Introducing
<a href=
"../../Libraries/JITLib/Patterns/EventPatternProxy.html"><span class=
"s4">EventPatternProxy
</span></a> into the mix makes it possible to swap different melody and rhythm components in and out on the fly, with no interruption. We can even change the type of pattern (Pbind, Pmono, PmonoArtic) with no ill effect.
</p>
83 <p class=
"p3"><br></p>
84 <p class=
"p8"><span class=
"s3">~rhythm =
</span>EventPatternProxy
<span class=
"s3">(
</span>Pbind
<span class=
"s3">(
</span></p>
85 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s5">\dur
</span>,
<span class=
"s2">Pwrand
</span>(#[
0.125,
0.25,
0.5], #[
0.3,
0.5,
0.2],
<span class=
"s2">inf
</span>),
</p>
86 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s5">\legato
</span>,
<span class=
"s2">Pwrand
</span>(#[
0.1,
0.6,
1.01], #[
0.1,
0.3,
0.6],
<span class=
"s2">inf
</span>)
<span class=
"Apple-converted-space"> </span></p>
88 <p class=
"p6"><br></p>
89 <p class=
"p8"><span class=
"s3">~melody =
</span>EventPatternProxy
<span class=
"s3">(
</span>Pbind
<span class=
"s3">(
</span></p>
90 <p class=
"p5"><span class=
"Apple-tab-span"> </span><span class=
"s5">\degree
</span>,
<span class=
"s2">Pwhite
</span>(-
4,
11,
<span class=
"s2">inf
</span>)
</p>
92 <p class=
"p6"><br></p>
93 <p class=
"p5">p =
<span class=
"s2">Pchain
</span>(~melody, ~rhythm).play;
</p>
94 <p class=
"p6"><br></p>
95 <p class=
"p5">~melody.source =
<span class=
"s2">PmonoArtic
</span>(
<span class=
"s5">\default
</span>,
<span class=
"s5">\degree
</span>,
<span class=
"s2">Pseries
</span>(
4,
<span class=
"s2">Prand
</span>(#[-
1,
1],
<span class=
"s2">inf
</span>),
<span class=
"s2">inf
</span>).fold(-
4,
11));
</p>
96 <p class=
"p6"><br></p>
97 <p class=
"p5">~melody.source =
<span class=
"s2">Pbind
</span>(
<span class=
"s5">\degree
</span>,
<span class=
"s2">Pseries
</span>(
4,
<span class=
"s2">Pwrand
</span>(#[-
2, -
1,
1,
2], #[
0.3,
0.2,
0.2,
0.3],
<span class=
"s2">inf
</span>),
<span class=
"s2">inf
</span>).fold(-
4,
11));
</p>
98 <p class=
"p6"><br></p>
99 <p class=
"p5">p.stop;
</p>
100 <p class=
"p3"><br></p>
101 <p class=
"p3"><br></p>
102 <p class=
"p2"><b>Pset and cousins
</b></p>
103 <p class=
"p3"><br></p>
104 <p class=
"p4">A group of pattern classes allow single event keys to be overwritten, or one addition or multiplication to be performed. Pkey, in combination with the Pchain or Pbindf
"pattern composition" classes, can do everything the following classes can do (though this alternate notation may be more convenient in certain cases).
</p>
105 <p class=
"p3"><br></p>
106 <p class=
"p4"><b>Pset(name, value, pattern):
</b> Get one event from 'pattern', and then put the next value from the 'value' pattern into the 'name' key. If the source pattern specifies a value for the same name, the value from the source will be replaced with the new one.
</p>
107 <p class=
"p4"><b>Padd(name, value, pattern):
</b> After getting the next event, replace the 'name' value with its existing value + the next number from 'value'.
</p>
108 <p class=
"p4"><b>Pmul(name, value, pattern):
</b> After getting the next event, replace the 'name' value with its existing value * the next number from 'value'.
</p>
109 <p class=
"p3"><br></p>
110 <p class=
"p4">These patterns remain in the library mainly for reasons of backward compatibility, since their behavior can be replicated easily using Pbindf.
</p>
111 <p class=
"p3"><br></p>
112 <p class=
"p4"><b>Pset
</b>(name, value, pattern) ==
<b>Pbindf
</b>(pattern,
<b>name, value
</b>)
</p>
113 <p class=
"p4"><b>Padd
</b>(name, value, pattern) ==
<b>Pbindf
</b>(pattern,
<b>name, Pkey(name) + value
</b>)
</p>
114 <p class=
"p4"><b>Pmul
</b>(name, value, pattern) ==
<b>Pbindf
</b>(pattern, name,
<b>Pkey(name) * value
</b>)
</p>
115 <p class=
"p3"><br></p>
116 <p class=
"p4">Psetpre, Paddpre, and Pmulpre reverse the order of evaluation. Pchain is able to duplicate this functionality.
</p>
117 <p class=
"p3"><br></p>
118 <p class=
"p4"><b>Psetpre(name, value, pattern):
</b> Get the next 'value' and put it into the event prototype before evaluating 'pattern'.
</p>
119 <p class=
"p3"><br></p>
120 <p class=
"p4"><b>Psetpre
</b>(name, value, pattern) ==
<b>Pchain
</b>(pattern,
<b>Pbind(name, value)
</b>);
</p>
121 <p class=
"p4"><b>Paddpre
</b>(name, value, pattern) ==
<b>Pchain
</b>(pattern,
<b>Pbind(name, Pkey(name) + value)
</b>);
</p>
122 <p class=
"p4">Similar for Pmulpre
</p>
123 <p class=
"p3"><br></p>
124 <p class=
"p4">A third group --
<a href=
"../Patterns/Psetp.html"><span class=
"s6">Psetp
</span></a>,
<a href=
"../Patterns/Paddp.html"><span class=
"s6">Paddp
</span></a>,
<a href=
"../Patterns/Pmulp.html"><span class=
"s6">Pmulp
</span></a> -- behave slightly differently, nesting pattern evaluation.
</p>
125 <p class=
"p3"><br></p>
126 <p class=
"p3"><br></p>
127 <p class=
"p9"><span class=
"s3">Previous:
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><a href=
"PG_06b_Time_Based_Patterns.html"><span class=
"s7">PG_06b_Time_Based_Patterns
</span></a></span></p>
128 <p class=
"p9"><span class=
"s3">Next:
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><a href=
"PG_06d_Parallel_Patterns.html"><span class=
"s7">PG_06d_Parallel_Patterns
</span></a></span></p>